package com.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车管理")
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "添加购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("skuNum") Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return AjaxResult.success();
    }

    @Operation(summary = "查询用户购物车所有数据")
    @GetMapping("/cartList")
    public AjaxResult getAllCart() {
        return AjaxResult.success(cartService.getAllCart());
    }

    @Operation(summary = "删除购物车中的商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return AjaxResult.success();
    }

    @Operation(summary = "更新购物车商品选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId,
                                @PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return AjaxResult.success();
    }

    @Operation(summary = "购物车商品全选或全不选")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked") Integer isChecked) {
        cartService.checkCart(null, isChecked);
        return AjaxResult.success();
    }

    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart() {
        cartService.deleteCart(null);
        return AjaxResult.success();
    }

    /**
     * 获取用户购物车选中的商品列表
     *
     * @return
     */
    @InnerAuth
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList(){
        return R.ok(cartService.getCartCheckedList());
    }

    /**
     * 更新购物车中商品的实时价格
     *
     * @return
     */
    @InnerAuth
    @GetMapping("/updateCartPrice")
    public R updateCartPrice() {
        cartService.updateCartPrice();
        return R.ok();
    }

    /**
     * 删除已经完成下单的购物车商品
     *
     * @return
     */
    @InnerAuth
    @GetMapping("/deleteCartCheckedList")
    R deleteCartCheckedList() {
        cartService.deleteCartCheckedList();
        return R.ok();
    }
}
